/**
 * 主页JS
 */
/**
 * 页面初始化方法
 */
var initPage = function() {
	var searchText = $('.search_text'),
		searchBtn = $('.search_btn'),
		creatButton = $('.creat_button');
	searchText.bind({
		focus: function(){
			var placeholder = searchText.attr('placeholder');
			if(placeholder=='输入关键字搜索群'){
				searchText.attr('placeholder', '');
			}
		},
		blur: function(){
			var placeholder = searchText.attr('placeholder');
			if(placeholder==''){
				searchText.attr('placeholder', '输入关键字搜索群');
			}
		}
	});
	searchBtn.click(function(){
		loadData(iscrollLoadData.params);
	});
	creatButton.click(function(){
		window.location.href = SystemConfig.baseHtmlUrl+"createGroup.html";
	});
	$.ajax({
		type : "POST",
		url : SystemConfig.baseServiceUrl + "group/queryTypes",
		contentType : "application/json; charset=UTF-8",
		dataType : "json",
		success : function(data) {
			if(data&&data.groupTypeList){
				var list = $.parseJSON(data.groupTypeList);
				var html ='<span class="total">全部</span>';
				for(var i=0; i<list.length; i++) {
					var groupType = list[i];
					html += '<span data="'+groupType.value+'">'+groupType.display+'</span>';
				}
				$(".fix_1_top").after(html);
				$.each($(".fix_1 > span"), function(index, groupType){
					$(groupType).on("click",function(){
						$(".fix_1 .total").removeClass("total");
						$(groupType).addClass("total");
						$(".fix_1").hide();
						resetParams();
						var data = $(groupType).attr('data');
						if(data){
							iscrollLoadData.params.groupType = new Number(data);
						}else{
							iscrollLoadData.params.groupType = null;
						}
						loadData(iscrollLoadData.params);
						
						$(".classify span").text($(groupType).text());
						
					});
				});
			}
			if(data&&data.sortTypeList){
				var list = $.parseJSON(data.sortTypeList);
				var html ='<span class="total">全部</span>';
				for(var i=0; i<list.length; i++) {
					var sortType = list[i];
					html += '<span data="'+sortType.value+'">'+sortType.display+'</span>';
				}
				$(".fix_2_top").after(html);
				$.each($(".fix_2 > span"), function(index, sortType){
					$(sortType).on("click",function(){
						$(".fix_2 .total").removeClass("total");
						$(sortType).addClass("total");
						$(".fix_2").hide();
						resetParams();
						var data = $(sortType).attr('data');
						if(data){
							iscrollLoadData.params.sortType = new Number(data);
						}else{
							iscrollLoadData.params.sortType = null;
						}
						loadData(iscrollLoadData.params);
						
						$(".sort span").text($(sortType).text());
					});
				});
			}
		},
		exception : function(resp) {
			var message = resp.responseStatus.message;
			alert(message);
		}
	});
	$(".classify").click(function(){
        $(".fix_1").toggle();
        $(".fix_2").hide();
    });
    $(".fix_1 span").click(function(){
        $(".fix_1").hide();
    });
    $(".sort").click(function(){
        $(".fix_2").toggle();
        $(".fix_1").hide();
    });
    $(".fix_2 span").click(function(){
        $(".fix_2").hide();
    });
};

//获取群信息列表
var groupInfoList = function(callBack){
	var searchText = $('.search_text'),text = searchText.val();
	if(!SystemUtil.isNull(text)){
		iscrollLoadData.params.groupNameLike = text;
	}else{
		iscrollLoadData.params.groupNameLike = null;
	}
	$.ajax({
		type: "post",
		url: SystemConfig.baseServiceUrl + 'group/queryGroupInfoList',
		dataType :"json",
		contentType: "application/json; charset=UTF-8",
		data: $.toJSON(iscrollLoadData.params),
		success : function(data){
			var list = null,
				html ="";
			if(data&&data.genericPage&&data.genericPage.data){
				list = data.genericPage.data;
				iscrollLoadData.params.hasPreviousPage = data.genericPage.hasPreviousPage;
				iscrollLoadData.params.hasNextPage = data.genericPage.hasNextPage;
				for(var i=0; i<list.length; i++) {
					var groupShareInfo = list[i];
					html += '<li id="'+groupShareInfo.id+'"><div class="qun_box"><a href="javascript:void(0);" class="qun_a">' + 
					'<img src="'+ SystemUtil.buildImageUrl(groupShareInfo.avatarUrl, "64x64") +'" class="qun_wei">' +
					'<h2 class="qun_h2">'+ groupShareInfo.groupName  +'</h2>' +
					'<p class="qun_text">' + groupShareInfo.details + '</p>' + 
					'<div class="add_qun"><button class="add_button">加群</button></div></a></div></li>';
				}
			}
			callBack(html);
			//$("#groupList").append(html);
			$.each($("#groupList li"), function(index, li){
				var groupId = $(li).attr("id");
				$(li).on("click",function(){
					window.location.href = SystemConfig.baseHtmlUrl + 'groupDetail.html?groupId=' + groupId;
				});
			});
		}
	});
}

var iscrollLoadData = null;

var loadData = function(params){
	if(params){
		iscrollLoadData.params = params;
	}else{
		iscrollLoadData.params = {
				groupNameLike : null,
				groupType : null,
				sortType: null,
				limit : 20,
				start : 0,
				hasPreviousPage : false,
				hasNextPage : false
		};
	}
	groupInfoList(function(html){
		$("#groupList").empty().append(html);
		iscrollLoadData.refresh();
		$(iscrollLoadData.pullUpEl).hide();
		$(iscrollLoadData.pullDownEl).hide();
	});
};

var resetParams = function(){
	iscrollLoadData.params.limit = 20;
	iscrollLoadData.params.start = 0;
	iscrollLoadData.params.hasPreviousPage = false;
	iscrollLoadData.params.hasNextPage = false;
};

$(function () {
	// 初始化页面
	initPage();
	function dropTopAction(cb){
		setTimeout(function(){
			cb();
		},800);
	}
	function dropBottomAction(cb){
		setTimeout(function(){
			if (iscrollLoadData.params.hasNextPage) {
				iscrollLoadData.params.start = iscrollLoadData.params.start + iscrollLoadData.params.limit;
				groupInfoList(cb);
			}else{
				cb();
			}
		},800);
	}
	function loaded(){
		var wrapper=document.querySelector('#wrapper');
		var content=document.querySelector('#groupList');
		var params = {
			groupNameLike : null,
			groupType : null,
			sortType: null,
			limit : 20,
			start : 0,
			hasPreviousPage : true,
			hasNextPage : false
		};
		iscrollLoadData = new IScrollLoadData(wrapper,content,dropTopAction,dropBottomAction,params);
		loadData();
		document.onkeydown = function(evt){
			if(window.event){
				evt = window.event;
			}
			if(evt.keyCode==13){
				loadData(iscrollLoadData.params);
			}
		};
	}
	document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
	loaded();
});